package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starsea.book.databinding.ItemBookListBinding
import com.starsea.book.ui.newpage.entity.BookInfoEntity
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.utils.loadNormalImage


/**
 * @author Lee
 * @createTime 2024-07-12 20 GMT+8
 * @desc :
 */
class SearchBookResultAdapter(var mContext: Context) :
    BaseQuickAdapter<BookInfoEntity, SearchBookResultAdapter.MyViewHolder>() {

    var keyword = ""
    override fun onBindViewHolder(holder: MyViewHolder, position: Int, item: BookInfoEntity?) {
        holder.binding.ivCover.loadNormalImage(mContext, item!!.bookCover ?: "")


        if (TextUtils.isEmpty(keyword)) {
            holder.binding.tvTitle.text = item.bookTitle
            holder.binding.tvContent.text = item.bookDesc
        } else {
            val spannableString = SpannableString(item.bookTitle)
            val highlightSpan = ForegroundColorSpan(Color.RED)
            LogUtils.e(">>> keyword = $keyword")

            keyword.split("")
                .forEach {
                    LogUtils.e(">>> $it")
                    val index = item.bookTitle!!.indexOf(it)
                    LogUtils.e(">>> index = $index")
                    if (index >= 0) {
                        spannableString.setSpan(
                            highlightSpan,
                            index,
                            index + it.length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    }
                }

            holder.binding.tvTitle.text = spannableString

            val spannableDescString = SpannableString(item.bookDesc)
            keyword.split("")
                .forEach {
                    val index = item.bookDesc!!.indexOf(it)
                    if (index >= 0) {
                        spannableDescString.setSpan(
                            highlightSpan,
                            index,
                            index + it.length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    }
                }

            holder.binding.tvContent.text = spannableDescString

        }





        holder.binding.tvScore.text = item.bookScore.finialDouble()

        holder.binding.tvContentHint.text =
            "${
                item.activityLabel.replace(
                    "@",
                    "·"
                )
            }·${if (item.bookState == 0) "连载" else if (item.bookState == 1) "完结" else "停更"}"

    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int,
    ): MyViewHolder {
        return MyViewHolder(parent = parent)
    }

    class MyViewHolder(
        parent: ViewGroup,
        val binding: ItemBookListBinding = ItemBookListBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)


    fun highLight(key: String, spannableString: SpannableString, content: String) {
        // 创建一个ForegroundColorSpan对象，用于设置高亮颜色


    }

}